<?php


/**
 * Notes:
 * PROJECT_NAME : ERP v1.8 重构erp
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2024/1/15
 * Time: 16:26
 */

namespace app\admin\traits;

use Exception;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Request;
use think\Response;

trait Crud
{


    /**
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function list(): Response
    {
        $param = Request::param();
        if($this->store_id)$param['store_id'] = $this->store_id;//添加商品查询条件
        validate($this->validateClass)->scene('list')->check($param);
        $sql = $this->model;
        if ($this->withLink) $sql = $sql->with(explode(",", $this->withLink));
        $sql = $sql->withSearch($this->searchFields, $param);
        if ($this->fieldSearch) $sql = $sql->field($this->fieldSearch);
        $count = $sql->count();
        if (Request::param('is_export', 0) != 1) $sql = $sql->page($param['page'], $param['limit']);
        $list = $sql->order($this->orderSort)->select();
        if ($this->isEach) $list = $this->format($list);
        if ($this->searchAppendField) $list = $list->append(explode(',', $this->searchAppendField));
        if ($this->searchHiddenField) $list = $list->hidden(explode(',', $this->searchHiddenField));
        return $this->success('获取成功', compact('count', 'list'));
    }

    /**
     * 新增
     * @return Response
     */
    public function add(): Response
    {
        $param = Request::param();
        validate($this->validateClass)->scene('add')->check($param);
        $this->model->startTrans();
        try {
            $this->model->create($param);
        } catch (\Exception $e) {
            $this->model->rollback();
            return $this->error('创建失败' . $e->getMessage());
        }
        $this->model->commit();
        return $this->success('创建成功');
    }

    /**
     * 编辑
     * @return Response
     */
    public function edit(): Response
    {
        $param = Request::param();
        validate($this->validateClass)->scene('edit')->check(Request::param());
        $this->model->startTrans();
        try {
            $sql = $this->model;
            if (!$this->isWithoutGlobalScope) $sql = $sql->withoutGlobalScope();
            if (!$info = $sql->where('id', $param['id'])->find()) exception('数据不存在');
            $this->uniqueField($this->model, $this->checkUniqueField, $param, $param['id']);
            unset($param['id']);
            if (empty($param)) exception('请填写要修改的内容');
            $info->save($param);
            $this->model->commit();
        } catch (Exception $e) {
            $this->model->rollback();
            return $this->error($e->getMessage());
        }
        return $this->success('修改成功');
    }


    /**
     * 编辑状态
     * @return Response
     */
    public function editStatus(): Response
    {
        $param = Request::param();
        validate($this->validateClass)->scene('editStatus')->check(Request::param());
        $this->model->startTrans();
        try {
            $sql = $this->model;
            if (!$info = $sql->where('id', $param['id'])->find()) exception('数据不存在');
            unset($param['id']);
            if (empty($param)) exception('请填写要修改的内容');
            $info->save($param);
            $this->model->commit();
        } catch (Exception $e) {
            $this->model->rollback();
            return $this->error($e->getMessage());
        }
        return $this->success('修改成功');
    }

    /**
     * 真删除
     * @return Response
     */
    public function del(): Response
    {
        $param = Request::param();
        validate($this->validateClass)->scene('del')->check(Request::param());
        $this->model->startTrans();
        try {
            $sql = $this->model;
            if (!$this->isWithoutGlobalScope) $sql = $sql->withoutGlobalScope();
            $this->model->destroy(explode(',', $param['id']));
            $this->model->commit();
        } catch (Exception $e) {
            $this->model->rollback();
            return $this->error($e->getMessage());
        }
        return $this->success('删除成功');
    }


    /**
     * 详情 返回数据不合理，自行修改
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function detail(): Response
    {
        $param = Request::param();
        validate($this->validateClass)->scene('detail')->check($param);
        $sql = $this->model;
        if ($this->detailWithLink) $sql = $sql->with(explode(",", $this->detailWithLink));
        $sql  = $sql->withSearch($this->searchFields, $param);
        $list = $sql->order($this->orderSort)->find();
        return $this->success('获取成功', compact('list'));
    }

    /**
     * 组装数据
     * @param $list
     * @return void
     */
    public function format($list): void
    {

    }
}